<?php
class AccountDataForm extends BasePublicUserForm
{


   public function configure()
   {

		$this->setNameFormat();
		$this->useFields(array( 'email', 'password' ));


		$this->widgetSchema['password'] = new sfWidgetFormInputPassword();
		$this->widgetSchema['password_confirm'] = new sfWidgetFormInputPassword();
		$this->widgetSchema['password_current'] = new sfWidgetFormInputPassword();

		$this->validatorSchema['password'] =  new sfValidatorString(array('required' => true), array('required' => 'Password required'));
		$this->validatorSchema['password_current'] =  new sfValidatorCallback(array(
			'required' => true,
			'callback' => array($this, 'validateCurrentPassword')
		), array(
			'required' => 'Enter current password',
			'invalid' => 'Wrong current password'
		));

		$this->validatorSchema->setPostValidator(
			new sfValidatorDoctrineUnique(array('model' => 'PublicUser', 'column' => array('email')), array('invalid' => 'Duplicate email.'))
		);
		
		$this->validatorSchema['password_confirm'] = new sfValidatorString(array(),array('required' => 'Password confirmation required','invalid' => 'Wrong pass.'));

		$this->validatorSchema->setPostValidator(
		  new sfValidatorSchemaCompare(
			'password',
			sfValidatorSchemaCompare::EQUAL,
			'password_confirm',
			array(),
			array('invalid' => 'Passwords does not match.')
		  )
		);


   }

   public function validateCurrentPassword($validator, $value){
		$user = sfContext::getInstance()->getUser()->getObject();

		if($user){
			if(!($user = Doctrine::getTable('PublicUser')->findOneByCredentials($user['email'], $value))){
				throw new sfValidatorError($validator, 'invalid');
			}else{

			}
		}else{
			throw new sfValidatorError($validator, 'invalid');
		}


		
   }

   public function setNameFormat(){
   		$this->widgetSchema->setNameFormat('account_data[%s]');
   }
}